package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Stack;

/**
 179. 最大数
 给定一组非负整数，重新排列它们的顺序使之组成一个最大的整数。
 示例 1:
 输入: [10,2]
 输出: 210
 示例 2:
 输入: [3,30,34,5,9]
 输出: 9534330
 说明: 输出结果可能非常大，所以你需要返回一个字符串而不是整数。
 */

public class problems_179 {

    public static void main(String[] arge) {
        System.out.println(new Solution().largestNumber(new int[]{10,2}));
    }

    static class Solution {
        private class LargerNumberComparator implements Comparator<String> {
            @Override
            public int compare(String a, String b) {
                String order1 = a + b;
                String order2 = b + a;
                return order2.compareTo(order1);
            }
        }

        public String largestNumber(int[] nums) {
            String[] asStrs = new String[nums.length];
            for (int i = 0; i < nums.length; i++) {
                asStrs[i] = String.valueOf(nums[i]);
            }
            Arrays.sort(asStrs, new LargerNumberComparator());
            if (asStrs[0].equals("0")) {
                return "0";
            }
            String largestNumberStr = new String();
            for (String numAsStr : asStrs) {
                largestNumberStr += numAsStr;
            }
            return largestNumberStr;
        }
    }
}